package com.wantup.springboot.boot.daoru.jxl;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

import java.io.File;
import java.io.IOException;

/**
 * @author Zhaoluowei
 * @version 1.0
 * @date 2018/11/30 11:35
 */
public class JxlExpExcel {


    public static void main(String[] args) throws IOException, WriteException {
        //数组存放表头数据
        String[] title = new String[]{"id", "name", "sex", "email", "address"};

        //创建excel 文件
        File file = new File("C:/Users/15010/Desktop/file/jxl_test.xls");
        if (!file.exists()) {
            file.createNewFile();
        }


        //创建工作簿
        WritableWorkbook workbook = Workbook.createWorkbook(file);
        //创建sheet页面
        WritableSheet sheet = workbook.createSheet("sheet_one", 0);
        Label label = null;
        for (int i = 0; i < title.length; i++) {
            //设置表头行列
            label = new Label(i, 0, title[i]);
            sheet.addCell(label);
        }

        //添加数据 从第二行开始
        for (int i = 1; i < 10; i++) {
            label = new Label(0, i, "" + i);
            sheet.addCell(label);
            label = new Label(1, i, "name_" + i);
            sheet.addCell(label);
            label = new Label(2, i, "sex_" + i);
            sheet.addCell(label);
            label = new Label(3, i, "email_" + i);
            sheet.addCell(label);
            label = new Label(4, i, "address_" + i);
            sheet.addCell(label);
            //写入数据
            workbook.write();
            workbook.close();

        }

    }

}
